package com.baosight.sczx.zm.cl.service;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cl.RoundCheckWorkNote;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCL;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cl.domain.ZMCLTernateRecord;

/**
 * 隐患三违录入模块
 * 三违录入
 * <p>Title: ServiceZMCL41.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-17 上午02:22:12  严洪浩  Create
 *</pre>
 */
public class ServiceZMCL41 extends ServiceEPBase {
 
	
	/**
	 * 当前页面托管加载初始化操作
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午10:20:17 高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo info )
	{
		EiInfo outInfo = this.query(info);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.classify"));//三违分类
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.sourceClassify"));//三违来源
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cl.workNum", false));//班次
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", false));//三违单位
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cl.responsibilityPeople", "empCode", "empName", false));//检查人
		//outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cl.workFace", true));//所在工作面
		//outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cl.roadWay", true));//所在巷道
		outInfo.addBlock(RoundCheckWorkNote.getAllNoteCode(null, true));//巡检工单编号
		
		return outInfo;
	}
	
	/**
	 * 页面进入查询当前录入的所有三违
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-17上午10:20:17 高强
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		String fdCreateUserId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();
		StaticTools.setQueryParameters(inInfo, "fdCreateUserId", fdCreateUserId, true);//添加查询条件 操作用户
		StaticTools.setQueryParameters(inInfo, "fdDate", StaticTools.getDate(), true);//添加查询条件 当前日期
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.limitStr, 100);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.isCountFlag, true);
		EiInfo outInfo = super.query(inInfo, "ZMCLTernateRecord.query", new ZMCLTernateRecord(), false, null, EiConstant.queryBlock, EiConstant.resultBlock,EiConstant.resultBlock);
		return outInfo;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-4-12 上午   高强  
	 * </p>
	 *
	 */
	
	public EiInfo delete(EiInfo inInfo) 
	{
		if(!"未处理".equals(inInfo.getBlock("result").getCellStr(0, "fdStatus"))) {
			inInfo.setStatus(-1);
			inInfo.setMsg("只能删除未处理的三违记录！");
			return inInfo;
		}
		EiInfo outInfo = super.delete(inInfo, "ZMCLTernateRecord.delete");
		return outInfo;
	}
	
	/**
	 * 新增隐患
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午   高强  
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo)
	{
		EiBlock _block = inInfo.getBlock("result");
		String fdKeyId = TablePk.getInstance().getNextValue("T_CL_TERNATE_RECORD");
		
        //在"三违记录表"中新增一条记录
		String fdManageDepartmentId = SystemSettingCL.getInstance().getManageDepartId();
		String fdManageDepartment = Department.getInstance().getDepartmentNameById(fdManageDepartmentId);
		
		if(fdManageDepartmentId != null && !"".equals(fdManageDepartmentId) && null != fdManageDepartment && !"".equals(fdManageDepartment))
		{
			String fdCheckPeopleId = _block.getCellStr(0, "fdCheckPeopleId");//检查人ID
			
			_block.setCell(0, "fdKeyId", fdKeyId);
			_block.setCell(0, "fdManageDepartmentId", fdManageDepartmentId);
			_block.setCell(0, "fdManageDepartment", fdManageDepartment);
			_block.setCell(0, "fdCheckPeople", fdCheckPeopleId);//检查人
			_block.setCell(0, "fdStatus", "未处理");
			_block.setCell(0, "fdIsPenalty", "否");
			_block.setCell(0, "fdCreateTime", StaticTools.getDateTime());
			_block.setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
			_block.setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
			
			EiInfo outInfo = super.insert(inInfo, "ZMCLTernateRecord.insert");
			
			return outInfo;
		}
		else {
			inInfo.setStatus(-1);
			inInfo.setMsg("基础数据中未设置安全管理单位！");
			return inInfo;
		}
	}

	/**
	 * 更新三违信息
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-12上午   高强  
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo) 
	{		
        if(!"未处理".equals(inInfo.getBlock("result").getCellStr(0, "fdStatus"))) {
			inInfo.setStatus(-1);
			inInfo.setMsg("只能修改未处理的三违记录！");
			return inInfo;
		}
		else {
			String fdManageDepartmentId = SystemSettingCL.getInstance().getManageDepartId();
			String fdManageDepartment = Department.getInstance().getDepartmentNameById(fdManageDepartmentId);
			
			if(fdManageDepartmentId != null && !"".equals(fdManageDepartmentId) && null != fdManageDepartment && !"".equals(fdManageDepartment))
			{
				String fdCheckPeopleId = inInfo.getBlock("result").getCellStr(0, "fdCheckPeopleId");//检查人ID
				
				inInfo.getBlock("result").setCell(0, "fdManageDepartmentId", fdManageDepartmentId);
				inInfo.getBlock("result").setCell(0, "fdManageDepartment", fdManageDepartment);
				inInfo.getBlock("result").setCell(0, "fdCheckPeople", fdCheckPeopleId);//检查人
				inInfo.getBlock("result").setCell(0, "fdStatus", "未处理");
				inInfo.getBlock("result").setCell(0, "fdIsPenalty", "否");
				inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());
				inInfo.getBlock("result").setCell(0, "fdCreateUserId", UserSession.getUserId());//录入人ID
				inInfo.getBlock("result").setCell(0, "fdCreateUser", UserSession.getInSessionProperty("UserCname"));//录入人
				
				EiInfo outInfo = super.update(inInfo, "ZMCLTernateRecord.update");
				return outInfo;
			}
			else {
				inInfo.setStatus(-1);
				inInfo.setMsg("基础数据中未设置安全管理单位！");
				return inInfo;
			}
		}
	}	
	
}
